package EXEX;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.naming.ldap.Rdn;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import network.MultiChatClientThread;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.SwingConstants;

public class rspClient extends JFrame implements Runnable{
	
	Socket socket;
	rspClientThread rct;
	int port = 4444;
	private JPanel contentPane;
	private JTextField ip;
	JTextField id;
	private JButton login;
	private JButton logout;
	JLabel result;  //종합 전적 표시 라벨
	private JLabel lblNewLabel_1;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	JButton sbtn;
	JButton rbtn;
	JButton pbtn;
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_3;
	JButton readybtn;
	JLabel userresult;
	
	JLabel time2;   // 남은 시간 표시창
	
	int rspselect2 = 0;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					rspClient frame = new rspClient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public rspClient() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(850, 0, 702, 570);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getIp());
		contentPane.add(getId());
		contentPane.add(getLogin());
		contentPane.add(getLogout());
		contentPane.add(getResult());
		contentPane.add(getLblNewLabel_1());
		contentPane.add(getLabel());
		contentPane.add(getLabel_1());
		contentPane.add(getLabel_2());
		contentPane.add(getSbtn());
		contentPane.add(getRbtn());
		contentPane.add(getPbtn());
		contentPane.add(getLblNewLabel_2());
		contentPane.add(getLblNewLabel_3());
		try {
			InetAddress ia = InetAddress.getLocalHost();
			setTitle("ip : " + ia.getHostAddress() + "  /  port : " + port);
			ip.setText(ia.getHostAddress());
			id.setText("손님");
			contentPane.add(getReadybtn());
			contentPane.add(getTime2());
			contentPane.add(getUserresult());
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	private JTextField getIp() {
		if (ip == null) {
			ip = new JTextField();
			ip.setHorizontalAlignment(SwingConstants.CENTER);
			ip.setBounds(124, 10, 116, 21);
			ip.setColumns(10);
		}
		return ip;
	}
	private JTextField getId() {
		if (id == null) {
			id = new JTextField();
			id.setHorizontalAlignment(SwingConstants.CENTER);
			id.setBounds(299, 10, 116, 21);
			id.setColumns(10);
		}
		return id;
	}

	private JButton getLogin() {
		if (login == null) {
			login = new JButton("접속");
			login.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serverConnect();
					login.setEnabled(false);
					logout.setEnabled(true);
					readybtn.setEnabled(true);
					id.setEnabled(false);
				}
			});
			login.setBounds(427, 9, 97, 23);
		}
		return login;
	}
	private JButton getLogout() {
		if (logout == null) {
			logout = new JButton("종료");
			logout.setEnabled(false);
			logout.setBounds(556, 9, 97, 23);
		}
		return logout;
	}
	private JLabel getResult() {
		if (result == null) {
			result = new JLabel("");
			result.setBounds(225, 41, 150, 25);
		}
		return result;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("나요");
			lblNewLabel_1.setBounds(65, 112, 97, 43);
		}
		return lblNewLabel_1;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("서버요");
			label.setBounds(470, 99, 90, 43);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("내가 냄");
			label_1.setBounds(65, 192, 150, 132);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("서버가 냄");
			label_2.setBounds(438, 206, 150, 132);
		}
		return label_2;
	}
	private JButton getSbtn() {
		if (sbtn == null) {
			sbtn = new JButton("가위");
			sbtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						rspselect2 = 1;
						rbtn.setEnabled(false);
						sbtn.setEnabled(false);
						pbtn.setEnabled(false);						
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			sbtn.setEnabled(false);
			sbtn.setBounds(12, 372, 150, 150);
		}
		return sbtn;
	}
	private JButton getRbtn() {
		if (rbtn == null) {
			rbtn = new JButton("바위");
			rbtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						rspselect2 = 2;
						rbtn.setEnabled(false);
						sbtn.setEnabled(false);
						pbtn.setEnabled(false);						
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			rbtn.setEnabled(false);
			rbtn.setBounds(185, 372, 150, 150);
		}
		return rbtn;
	}
	private JButton getPbtn() {
		if (pbtn == null) {
			pbtn = new JButton("보");
			pbtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						rspselect2 = 3;
						rbtn.setEnabled(false);
						sbtn.setEnabled(false);
						pbtn.setEnabled(false);						
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			pbtn.setEnabled(false);
			pbtn.setBounds(347, 372, 150, 150);
		}
		return pbtn;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("ID :");
			lblNewLabel_2.setBounds(259, 13, 32, 15);
		}
		return lblNewLabel_2;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("서버 IP");
			lblNewLabel_3.setBounds(55, 13, 57, 15);
		}
		return lblNewLabel_3;
	}
	
	private JButton getReadybtn() {
		if (readybtn == null) {
			readybtn = new JButton("Ready!!!");
			readybtn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ready();
				}
			});
			readybtn.setEnabled(false);
			readybtn.setBounds(509, 372, 165, 150);
		}
		return readybtn;
	}
	
	@Override
	public void run() {
		try{
			socket = new Socket(ip.getText(), port);
			rct = new rspClientThread(this, socket);
			rct.start();
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
	
	}
	public void serverConnect(){
		try{
			Thread t = new Thread(this);
			t.start();
			JOptionPane.showMessageDialog(null, ip.getText() + " 에 접속되었습니다.");
		}catch(Exception ex){
			ex.printStackTrace();
		}	
	}
	
	public void ready(){
		try{
			rspData rd = new rspData("ready", id.getText(), null);
			rct.send.writeObject(rd);
			readybtn.setEnabled(false);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	private JLabel getTime2() {
		if (time2 == null) {
			time2 = new JLabel("시간");
			time2.setBounds(185, 76, 263, 15);
		}
		return time2;
	}
	private JLabel getUserresult() {
		if (userresult == null) {
			userresult = new JLabel("New label");
			userresult.setBounds(566, 42, 57, 15);
		}
		return userresult;
	}
}
